package info.xiao.dataStructrue.stack;

import java.util.Arrays;
import java.util.Stack;

/**
 * @Classname SortedStack 栈排序
 * @Description 链接：https://leetcode.cn/problems/sort-of-stacks-lcci/
 * @Date 2022/10/20 15:52
 * @Created by 晓龙Oba
 */


public class SortedStack {
    private Stack<Integer> mainStack;
    private Stack<Integer> surportStack;

    public SortedStack() {
        this.mainStack = new Stack<>();
        this.surportStack = new Stack<>();
    }

    public void push(int val) {
        // 判断元素大小
        while (!mainStack.isEmpty()) {
            Integer pop = mainStack.pop();
            if (val < pop) {
                mainStack.push(pop);
                break;
            }
            surportStack.push(pop);
        }
        mainStack.push(val);
        while (!surportStack.isEmpty()) {
            mainStack.push(surportStack.pop());
        }
    }

    public void pop() {
        try {
            mainStack.pop();
        } catch (Exception e) {
        }
    }

    public int peek() {
        try {
            return mainStack.peek();
        } catch (Exception e) {
            return -1;
        }
    }

    public boolean isEmpty() {
        return mainStack.isEmpty();
    }


    public String[] execute(String[] instruction, Integer[] variable) {

        String[] result = new String[instruction.length];
        for (int i = 0; i < instruction.length; i++) {
            if (i == 29) {
                System.out.println("");
            }
            switch (instruction[i]) {
                case "SortedStack":
                    result[i] = "null";
                    break;
                case "peek":
                    result[i] = this.peek() + "";
                    break;
                case "pop":
                    this.pop();
                    result[i] = "null";
                    break;
                case "isEmpty":
                    result[i] = this.isEmpty() + "";
                    break;
                case "push":
                    this.push(variable[i]);
                    result[i] = "null";
                    break;
            }
        }

        return result;
    }

    public static void main(String[] args) {
        SortedStack sortedStack = new SortedStack();
        String[] instruction = {
                "SortedStack", "peek", "peek", "pop", "isEmpty", "peek", "push", "pop", "push", "peek", "push", "peek", "pop", "pop", "push", "isEmpty", "push", "peek", "isEmpty", "push", "peek", "peek", "isEmpty", "push", "isEmpty", "peek", "isEmpty", "pop", "peek", "pop", "push", "push", "isEmpty", "pop", "isEmpty", "peek", "push", "pop", "push", "push", "isEmpty", "pop", "pop", "push", "peek", "isEmpty", "pop", "peek", "push", "push", "peek", "isEmpty", "isEmpty", "isEmpty", "isEmpty", "isEmpty", "push", "push", "push", "push", "push", "peek", "peek", "isEmpty", "push"
        };
        Integer[] variable = {0, 0, 0, 0, 0, 0, 40, 0, 19, 0, 44, 0, 0, 0, 42, 0, 8, 0, 0, 29, 0, 0, 0, 25, 0, 0, 0, 0, 0, 0, 52, 63, 0, 0, 0, 0, 47, 0, 45, 52, 0, 0, 0, 17, 0, 0, 0, 0, 6, 30, 0, 0, 0, 0, 0, 0, 51, 46, 2, 56, 39, 0, 0, 0, 38};
        String[] execute = sortedStack.execute(instruction, variable);
        Arrays.stream(execute).forEach(item -> {
            System.out.print(item + ",");
        });
//        sortedStack.push(1);
//        sortedStack.push(3);
//        sortedStack.push(2);
//        sortedStack.push(0);
//        sortedStack.push(12);
//        sortedStack.push(13);
//        sortedStack.push(4);
    }

}
